<?php
/////////////////////////////////////////////////////////////////////////////////////
// xbtit - Bittorrent tracker/frontend
//
// Copyright (C) 2004 - 2007  Btiteam
//
//    This file is part of xbtit.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
//   1. Redistributions of source code must retain the above copyright notice,
//      this list of conditions and the following disclaimer.
//   2. Redistributions in binary form must reproduce the above copyright notice,
//      this list of conditions and the following disclaimer in the documentation
//      and/or other materials provided with the distribution.
//   3. The name of the author may not be used to endorse or promote products
//      derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
// IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
// TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
// EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
////////////////////////////////////////////////////////////////////////////////////
global $CURUSER;

if (!$CURUSER || $CURUSER["view_torrents"] == "no")
{

    // do nothing

}
else
{
    global $SITENAME, $XBTT_USE;


    function textLimit($string, $length, $replacer = '...')
    {

        if (strlen(trim($string)) > $length)
            return (preg_match('/^(.*)\W.*$/', substr($string, 0, $length + 1), $matches) ?
                $matches[1] : substr($string, 0, $length)) . $replacer;
        return $string;
    }

    $checkgolden = mysql_query("SELECT * FROM {$TABLE_PREFIX}gold LIMIT 0");

    if ($checkgolden)
        $isgold = " f.gold,";
    else
        $isgold = "";

    if ($XBTT_USE)
       {
        $tseeds="f.seeds+ifnull(x.seeders,0)";
        $tleechs="f.leechers+ifnull(x.leechers,0)";
        $tcompletes="f.finished+ifnull(x.completed,0)";
        $ttables="{$TABLE_PREFIX}files f LEFT JOIN xbt_files x ON x.info_hash=f.bin_hash";
       }
    else
        {
        $tseeds="f.seeds";
        $tleechs="f.leechers";
        $tcompletes="f.finished";
        $ttables="{$TABLE_PREFIX}files f";
        }

    $sql = "SELECT f.info_hash as hash, $tseeds as seeds, $tleechs as leechers, dlbytes AS dwned, $tcompletes as finished, filename, url, info, UNIX_TIMESTAMP(data) AS added, f.image as fimage, c.image, c.name AS cname, category AS catid, size, external, IF(anonymous='true','".$language['ANONYMOUS']."',uploader) AS uploader,$isgold u.username FROM $ttables LEFT JOIN {$TABLE_PREFIX}categories as c ON c.id = f.category LEFT JOIN {$TABLE_PREFIX}users as u ON f.uploader=u.id WHERE f.image != '' ORDER BY data DESC LIMIT 20";
        //$sql = "SELECT info_hash as hash, seeds, leechers, dlbytes AS dwned, format(finished,0) as finished, filename, url, info, UNIX_TIMESTAMP(data) AS added, f.image as fimage, c.image, c.name AS cname, category AS catid, size, external, uploader,$isgold u.username FROM {$TABLE_PREFIX}files as f LEFT JOIN {$TABLE_PREFIX}categories as c ON c.id = f.category LEFT JOIN {$TABLE_PREFIX}users as u ON f.uploader=u.id WHERE leechers + seeds > 0 AND f.image != '' ORDER BY data DESC LIMIT 20";

    $row = do_sqlquery($sql,true);

    if ($row)
    {
        $file = fopen("./lasttorrents.xml", "w");
        $result = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n<images title=\"" . $SITENAME . "\">\r\n";

        while ($data = mysql_fetch_array($row))
        {

            if (strlen($data["hash"]) > 0)
            {
                $data["filename"] = unesc($data["filename"]);

                $filename = $data["filename"];

                $pagelink = "index.php?page=torrent-details&amp;id=" . $data['hash'] . "";

                if ($data["external"] == "yes")
                    $golden = "ext";
                else
                {
                    if ($checkgolden)
                        $golden = $data["gold"];
                    else
                        $golden = "0";
                }

                $result .= "<pic>\r\n<thumb>torrentimg/" . $data["fimage"] . "</thumb>\r\n<link>" .
                    $pagelink . "</link>\r\n<category>" . $data["cname"] . "</category>\r\n<seeds>" .
                    $data["seeds"] . "</seeds>\r\n<leechers>" . $data["leechers"] . "</leechers>\r\n<finished>" .
                    $data["finished"] . "</finished>\r\n<golden>" . $golden . "</golden>\r\n<tooltip>" .
                    textLimit($data["filename"], 50) . "</tooltip>\r\n<uploader>" . $data["username"] .
                    "</uploader> />\r\n</pic>\r\n";
            }

        }

        $result .= "</images>";
        fwrite($file, utf8_encode($result));
        fclose($file);
    }
    block_begin(LAST_TORRENTS);
    ?>

<div align="center">
    <object classid=clsid:D27CDB6E-AE6D-11cf-96B8-444553540000
    codebase=http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=5,0,2,0
    width=600
    height=280>
    <param name=movie value="./lasttorrents.swf">
    <param name=quality value=high>
    <param name=SCALE value=showall>
    <param name=WMODE value=transparent>
    <embed src="./lasttorrents.swf"
    quality=high
    pluginspage=http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash type=application/x-shockwave-flash
    width=600
    height=280
    scale= showall
    wmode=transparent>
    </embed>
    </object>
</div>    
    <?php

    block_end();
}

?>